<style>
.pure_table_extra { width: 100%; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
.pure_table_extra img { cursor: pointer; margin-right: 2px; }
</style>

<?php 
$pg = Utils::get_param('pg', 1);
$q = Utils::get_param('q', 'DESC');
$o = Utils::get_param('o', 't.order_number');

if (isset($_SESSION['user'])) {
  $opts = new stdClass();
  if ($type == 1) {
    $opts->cd = 't.is_paid = :v1 AND t.is_cancel = :v0';
    $opts->cdv = array(':v1' => 0, ':v0' => 0);
  } else if ($type == 2) {
    $opts->cd = 't.is_paid = :v1 AND ship_date = :v2 AND t.is_cancel = :v0';
    $opts->cdv = array(':v1' => 1, ':v2' => '0000-00-00', ':v0' => 0);
  } else if ($type == 3) {
    $opts->cd = 't.ship_date != :v2 AND t.is_cancel = :v0';
    $opts->cdv = array(':v2' => '0000-00-00', ':v0' => 0);
  } else if ($type == 4) {
    $opts->cd = 't.is_cancel = :v0';
    $opts->cdv = array(':v0' => 1);
  }
    
  $opts->cd .=  ' AND (t.customer_id = :v3)';
  $opts->cdv[":v3"] = $user->id;
  $data = $orm->get('pb_ecom_order', $o, $q, $pg, SIZE, $opts);
}
?>


<table class="pure-table pure_table_extra" cellSpacing=0 cellPadding=0>
  <tr>
    <th class="ui-widget-header" style="width: 80px;">订单编号</th>
    <th class="ui-widget-header" style="width: 60px;">日期</th>
    <th class="ui-widget-header" style="width: 60px;">总价</th>
    <th class="ui-widget-header">收件人</th>
    <th class="ui-widget-header" style="width: 80px;">支付方式</th>
    <th class="ui-widget-header" style="width: 60px;">&nbsp;</th>
  </tr>
  <tbody id="data_body"></tbody>
</table>
<div>&nbsp;</div>
<div id="pagination"></div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __type = '<?php echo $type; ?>';
  
  $(function(){
    render();
  });

  function render() {
    $('#pagination').pagination({
      items: __data.t,
      itemsOnPage: __data.s,
      currentPage: __data.p,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
        return Url.redirect(__redmd, __sys + '.php?i=' + __id + '&inc=order&type=' + __type + '&pg=' + pageNumber + '&o=' + __data.o + '&q=' + __data.q);
      }
    });
    render_data();
  };
  
  function render_data() {
    Url.insert_param('o', __data.o);
    Url.insert_param('q', __data.q);
    
    $('#data_body').empty();
    for (var idx in __data.d) {
      var itm = __data.d[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.order_number + '</td>' +
          '<td>' + itm.date + '</td>' +
          '<td>' + accounting.formatMoney(parseFloat(itm.total, 10) + parseFloat(itm.delivery_subtotal, 10), itm.curr) + '</td>' +
          '<td title="' + itm.first_name + ' ' + itm.last_name + '">' + Utils.get_short(itm.first_name + ' ' + itm.last_name, 20) + '</td>' +
          '<td title="' + itm.payment_name + '">' + Utils.get_short(itm.payment_name, 10) + '</td>' +
          '<td>' +
            '<span title="详情" style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-zoomin" onclick="window.open(\'<?php echo URL . HOME_DIR . $app_data->sys->php_name . '.php?i=' . ECOM_ID . '&__file=index_order&od='; ?>' + itm.order_number + '\')"></span>' +
            (('<?php echo $type; ?>' == 1) ? '<span title="支付" style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-newwin" onclick="window.open(\'<?php echo URL . HOME_DIR . $app_data->sys->php_name . '.php?i=' . ECOM_ID . '&__file=index_paynow&od='; ?>' + itm.order_number + '\')"></span>' : '') +
            (('<?php echo $type; ?>' == 1) ? '<span title="取消" style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="cancel_obj(' + itm.id + ')"></span>' : '') +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.d.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=12>None</td></tr>');
    }
    $('#data_body input[type=checkbox]').button();
  };

  function cancel_obj(id) {
    var req = Utils.get_by_id(id, __data.d);
    req.is_cancel = 1;
    Ajax.request(
      __requrl + 'cancel',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        Utils.block_ui();
        location.reload();
      },
      { confirm: 1, tt: '确认', txt: '确认删除么？' }
    );
  };
  /*]]>*/
</script>